package com.example.dllo.keepdemo.train.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.tools.GsonRequest;
import com.example.dllo.keepdemo.tools.RecyclerViewOnClick;
import com.example.dllo.keepdemo.tools.RoundImageView;
import com.example.dllo.keepdemo.tools.VolleySingleton;
import com.example.dllo.keepdemo.train.activity.TrainDynamicActivity;
import com.example.dllo.keepdemo.train.activity.VideoActivity;
import com.example.dllo.keepdemo.train.bean.TrainPlanBean;
import com.example.dllo.keepdemo.train.bean.TrainPlanPioneerBean;

import java.io.Serializable;
import java.util.List;

/**
 * Created by dllo on 16/1/14.
 */
public class TrainPlanExpandableListViewAdapter extends BaseExpandableListAdapter {
    private Context context;
    private TrainPlanBean.DataEntity data;
    private TrainPlanPioneerBean pioneerData;


    public TrainPlanExpandableListViewAdapter(Context context, TrainPlanBean.DataEntity data) {
        this.context = context;
        this.data = data;
    }


    @Override
    public int getGroupCount() {
        return data.getWorkouts().size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_expandablelistview_group_activity_train_plan, parent, false);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.tvNumber.setText("第" + data.getWorkouts().get(groupPosition).getOrder() + "次");
        String content = "";
        for (int i = 0; i < data.getWorkouts().get(groupPosition).getTrainingpoints().size(); i++) {
            content = content + data.getWorkouts().get(groupPosition).getTrainingpoints().get(i).getName() + " ";
        }
        groupViewHolder.tvBody.setText(content);
        groupViewHolder.tvTime.setText(data.getWorkouts().get(groupPosition).getDuration() + "分钟");
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_expandablelistview_child_activity_train_plan, parent, false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        childViewHolder.adapter = new TrainPlanRecyclerViewAdapter(context,data.getWorkouts().get(groupPosition).getSteps());
        childViewHolder.recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL));
        childViewHolder.recyclerView.setAdapter(childViewHolder.adapter);

        childViewHolder.adapter.setOnClick(new RecyclerViewOnClick() {
            @Override
            public void OnClick(int position) {
                Toast.makeText(context, "position:" + position, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, VideoActivity.class);
                intent.putExtra("video",data.getWorkouts().get(groupPosition).getSteps().get(position).getExercise().getVideos().get(0).getUrl());
                intent.putExtra("content",data.getWorkouts().get(groupPosition).getSteps().get(position).getExercise().getDescription());
                intent.putExtra("title",data.getWorkouts().get(groupPosition).getSteps().get(position).getExercise().getName());
                context.startActivity(intent);
            }
        });
        //点击跳到训练动态
        childViewHolder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "点击", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, TrainDynamicActivity.class);
                intent.putExtra("id",data.getWorkouts().get(groupPosition).get_id());
                context.startActivity(intent);
            }
        });
        childViewHolder.tvFinishNumber.setText(data.getWorkouts().get(groupPosition).getTotalFinished() + "次完成");
        final ImageView[] images = {childViewHolder.imgHead1,childViewHolder.imgHead2,childViewHolder.imgHead3,childViewHolder.imgHead4,childViewHolder.imgHead5,childViewHolder.imgHead6,childViewHolder.imgHead7,childViewHolder.imgHead8,childViewHolder.imgHead9,childViewHolder.imgHead10};

        pioneerData = new TrainPlanPioneerBean();
        String url = "https://api.gotokeep.com/v1.1/workouts/" + data.getWorkouts().get(groupPosition).get_id()  + "/pioneer";
        GsonRequest<TrainPlanPioneerBean> gsonRequest = new GsonRequest<TrainPlanPioneerBean>(url, TrainPlanPioneerBean.class, new Response.Listener<TrainPlanPioneerBean>() {
            @Override
            public void onResponse(TrainPlanPioneerBean response) {
                pioneerData = response;
                for (int i = 0; i < response.getData().size(); i++) {
                    if (response.getData().get(i).getAvatar().equals("") || !(response.getData().get(i).getAvatar().endsWith(".jpg"))) {
                        images[i].setImageResource(R.mipmap.person_45_45);
                    } else {
                        ImageLoader.ImageListener listener = ImageLoader.getImageListener(images[i],R.mipmap.person_45_45,R.mipmap.person_45_45);
                        VolleySingleton.getImageLoader().get(response.getData().get(i).getAvatar(),listener,50,50);
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(gsonRequest);

        return convertView;
    }



    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class GroupViewHolder {
        private TextView tvNumber, tvBody, tvTime;

        public GroupViewHolder(View view) {
            tvNumber = (TextView) view.findViewById(R.id.tv_item_activity_trainplan_number);
            tvBody = (TextView) view.findViewById(R.id.tv_item_activity_trainplan_body);
            tvTime = (TextView) view.findViewById(R.id.tv_item_activity_trainplan_duration);
        }
    }

    class ChildViewHolder {
        private RecyclerView recyclerView;
        private RelativeLayout relativeLayout;
        private TrainPlanRecyclerViewAdapter adapter;
        private TextView tvFinishNumber;
        private RoundImageView imgHead1,imgHead2,imgHead3,imgHead4,imgHead5,imgHead6,imgHead7,imgHead8,imgHead9,imgHead10;

        public ChildViewHolder(View view) {
            recyclerView = (RecyclerView) view.findViewById(R.id.recycler_item_activity_trainplan);
            relativeLayout = (RelativeLayout) view.findViewById(R.id.relative_item_activity_trainplan);
            tvFinishNumber = (TextView) view.findViewById(R.id.tv_item_activity_train_plan_finish_number);
            imgHead1 = (RoundImageView) view.findViewById(R.id.roundimg_item_activity_trainplan_head_1);
            imgHead2 = (RoundImageView) view.findViewById(R.id.roundimg_item_activity_trainplan_head_2);
            imgHead3 = (RoundImageView) view.findViewById(R.id.roundimg_item_activity_trainplan_head_3);
            imgHead4 = (RoundImageView) view.findViewById(R.id.roundimg_item_activity_trainplan_head_4);
            imgHead5 = (RoundImageView) view.findViewById(R.id.roundimg_item_activity_trainplan_head_5);
            imgHead6 = (RoundImageView) view.findViewById(R.id.roundimg_item_activity_trainplan_head_6);
            imgHead7 = (RoundImageView) view.findViewById(R.id.roundimg_item_activity_trainplan_head_7);
            imgHead8 = (RoundImageView) view.findViewById(R.id.roundimg_item_activity_trainplan_head_8);
            imgHead9 = (RoundImageView) view.findViewById(R.id.roundimg_item_activity_trainplan_head_9);
            imgHead10 = (RoundImageView) view.findViewById(R.id.roundimg_item_activity_trainplan_head_10);
        }
    }

}
